package com.bdcom.autotest.ui.snp;

import com.bdcom.autotest.interaction.bean.DeviceUpdBean;
import com.bdcom.autotest.interaction.http.ConsoleServerOper;
import com.bdcom.autotest.interaction.http.OperDevice;
import com.bdcom.autotest.interaction.step.StepThread;
import com.bdcom.autotest.util.Prop;
import com.bdcom.autotest.util.UIUtil;
import com.bdcom.autotest.util.excel.FUtil;
import javafx.stage.FileChooser;
import org.apache.commons.collections.map.HashedMap;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;
import java.util.Map;

public class PanelForImportExcel extends AbsPanel {
	private boolean flag  = false;
	private boolean result  = false;
	private static Logger log = LoggerFactory.getLogger(PanelForImportExcel.class);
	public PanelForImportExcel(StepThread mainrun) {
		this.setBackground(Color.WHITE);
		this.setPreferredSize(new Dimension( 0 , 250 ));
//		JPanel p1 = UIUtil.createPanel();
//		Font font2 = new Font("Dialog",Font.PLAIN, 14);
//		JLabel jLabel0 = new JLabel( "选择文件");
//		jLabel0.setFont(font2);
//		JTextField wsn = new JTextField(18);
//		wsn.setName("0");
//		wsn.setFont(font2);
//		wsn.setText("");
//		p1.add(jLabel0);
//		p1.add(wsn);
//		p1.add(Box.createHorizontalStrut(10) );
		JPanel p2 = UIUtil.createPanel();
		JButton btn2 = new JButton("浏览");
		btn2.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser jfc=new JFileChooser();
				jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
				jfc.showDialog(new JLabel(), "选择");
				File file=jfc.getSelectedFile();
				if(file.isDirectory()){
					System.out.println("文件夹:"+file.getAbsolutePath());
				}else if(file.isFile()){
					System.out.println("文件:"+file.getAbsolutePath());
				}
//				wsn.setText(file.getAbsolutePath());
				List<DeviceUpdBean>  updBeans = parseExcel(file.getAbsolutePath());
				Map<String,DeviceUpdBean> macToUpdBean =new HashedMap();
				for(DeviceUpdBean updBean:updBeans){
					macToUpdBean.put(updBean.getMac(),updBean);
//					mainrun.addLog(updBean.printInfo());
				}
				if(updBeans.size()>0){
					mainrun.addLog("导入Excel成功!");
					log.info("导入Excel成功!");
					mainrun.getMf().getLeftp().setworkno(updBeans.get(0).getWorkOrderNum());
					mainrun.setWorkOrderNum(updBeans.get(0).getWorkOrderNum());
					mainrun.setMacToUpdBean(macToUpdBean);
//					mainrun.setBaud(updBeans.get(0).getOldBaud());
					mainrun.addLog("请等待设置波特率......");
					int setResult = ConsoleServerOper.setBaudRate(Prop.console_server_ip,updBeans.get(0).getOldBaud());//设置旧波特率
					if(setResult == ConsoleServerOper.SETRES_SUCCESS){
						log.info("设置波特率成功!");
						mainrun.addLog("设置波特率成功!");
						flag = true;
						result =true;
					}else if(setResult == ConsoleServerOper.SETRES_SETFAIL){
						log.info("设置波特率失败!");
						mainrun.addLog("设置波特率失败!");
						flag = true;
						result =false;
					}else if(setResult == ConsoleServerOper.SETRES_REBOOTFAIL){
						log.info("设备重启失败!");
						mainrun.addLog("设备重启失败!");
						flag = true;
						result =false;
					}else{
						flag = true;
						result =true;
					}
				}else{
					mainrun.addErrorLog("未获取到信息！");
					flag = true;
					result =false;
				}

			}
		});
//		p2.add(Box.createHorizontalStrut( 20 ));
		p2.add(btn2);
//		this.add(p1);
		this.add(p2);
	}


	public boolean getTotalResult(){
		while(!flag){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}

	private List<DeviceUpdBean>  parseExcel(String path){
		List<DeviceUpdBean> updBeans = FUtil.getUpdInfos(path);
		return  updBeans;
	}
}
